package com.bs.modules.wechat.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bs.common.utils.R;
import com.bs.modules.app.service.UserService;
import com.bs.modules.use.entity.AppComplaintEntity;
import com.bs.modules.use.entity.AppUserEntity;
import com.bs.modules.use.service.AppComplaintService;
import com.bs.modules.use.service.AppUserService;
import com.bs.modules.wechat.vo.VxLoginDTO;
import com.bs.modules.wechat.vo.VxSignInDTO;
import com.sun.deploy.services.PlatformType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @作者 ：lichenguang
 * @描述 ：【wechat用户信息】
 * @创建 ：2022/4/7 23:53
 */
@RestController
@RequestMapping("wechat/user")
@Api(tags = "wechat用户信息")
public class UserController {

  @Autowired
  private UserService userService;

  @Autowired
  private AppComplaintService appComplaintService;

  @SneakyThrows
  @ApiOperation(value = "获取微信授权信息")
  @PostMapping("/jscode2session")
  public R jscode2session(@RequestBody VxLoginDTO dto) {

    JSONObject ret = userService.jscode2session(dto.getAppId(), dto.getSecret(), dto.getJsCode());

    return R.ok().put("data", ret);
  }

  @SneakyThrows
  @ApiOperation(value = "获取微信绑定用户信息")
  @GetMapping("/findAppUser")
  public R findAppUser(@RequestParam Map<String, Object> params) {
    Object openid = params.get("openid");
    if (openid == null || "".equals(openid)) {
      return R.error("请先登录");
    }

    AppUserEntity appUserEntity = userService.findAppUser(openid);

    return R.ok().put("data", appUserEntity);
  }

  @SneakyThrows
  @ApiOperation(value = "微信绑定用户编号")
  @GetMapping("/bingUserId")
  public R bingUserId(@RequestParam Map<String, Object> params) {
    Object openid = params.get("openid");
    if (openid == null || "".equals(openid)) {
      return R.error("请先登录");
    }

    Object userId = params.get("userId");
    if (userId == null || "".equals(userId)) {
      return R.error("用户编号不能为空");
    }

    AppUserEntity appUserEntity = userService.findUserId(Long.parseLong(String.valueOf(userId).trim()));
    if (appUserEntity == null) {
      return R.error("查无此用户编号");
    }

    userService.bingUserId(openid, userId);

    return R.ok().put("data", "绑定成功");
  }


  @SneakyThrows
  @ApiOperation(value = "意见反馈")
  @PostMapping("/complaint")
  public R complaint(@RequestBody Map<String, Object> params) {

    Object openid = params.get("openId");
    if (openid == null || "".equals(openid)) {
      return R.error("请先登录");
    }

    appComplaintService.complaint(params.get("openId"), params.get("content"));

    return R.ok();
  }

  @SneakyThrows
  @ApiOperation(value = "意见反馈")
  @GetMapping("/getComplaint")
  public R getComplaint(@RequestParam Map<String, Object> params) {
    Object openid = params.get("openId");
    if (openid == null || "".equals(openid)) {
      return R.error("请先登录");
    }

    List<AppComplaintEntity> list = appComplaintService.getComplaint(openid);

    return R.ok().put("data", list);
  }
}
